package com.ax.api.route.associator.storeAssociator;


import com.ax.api.route.util.RouteConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/baseDrugstore/")
public class BaseDrugstoreController {

    @Autowired
    LoadBalancerClient loadBalancerClient;
    @Autowired
    RestTemplate restTemplate;

    /**
     * 药店药师列表
     * @param map
     * @return
     */
    @RequestMapping("queryDrugstore")
    public ResponseEntity<String> queryDoctor(@RequestParam Map<String,Object> map){
        String url = geturl("/web/baseDrugstore/queryDrugstore");
        return restTemplate.postForEntity(url,map,String.class);
    }

    @RequestMapping("pharmacistProfile")
    public ResponseEntity<String> queryPharmacistProfile(@RequestParam Long pharmacistId){
        String url = geturl("/web/baseDrugstore/pharmacistProfile");
        return restTemplate.postForEntity(url,pharmacistId,String.class);
    }


    private String geturl(String path){
        ServiceInstance serviceInstance = loadBalancerClient.choose(RouteConfig.ASSOCIATOR_ROUTE);
        //	 String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort()+path;
        StringBuffer bf = new StringBuffer("http://");
        bf.append(serviceInstance.getHost());
        bf.append(":");
        bf.append(serviceInstance.getPort());
        bf.append(path);
        return bf.toString();

    }

}
